package com.brayan.melinxa.gui.wizard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Component;
import javax.swing.Box;

public class Wizard extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<JPanel> listOfPanels = null;
	int idx = 0;

	/** This is the main panel that accepts "step" panels */
	JPanel panel_Content = new JPanel();
	JButton btnBack = new JButton("back");
	JButton btnNext = new JButton("next");
	JButton btnFinish = new JButton("finish");
	JFrame parentFrame = null;

	public Wizard(List<JPanel> listOfPanels, JFrame parentFrame) {
		this.listOfPanels = listOfPanels;
		this.parentFrame = parentFrame;
		init();
	}

	/**
	 * @wbp.parser.constructor
	 */
	public Wizard(JFrame parentFrame) {
		this.parentFrame = parentFrame;
		init();
	}

	private void init() {
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));

		JPanel panel_WizardBar = new JPanel();
		panel_WizardBar.setBackground(Color.WHITE);
		add(panel_WizardBar, BorderLayout.NORTH);
		panel_WizardBar.setLayout(new BoxLayout(panel_WizardBar, BoxLayout.Y_AXIS));

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel_WizardBar.add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		JTextPane textPaneWizardTitle = new JTextPane();
		textPaneWizardTitle.setContentType("text/html");
		textPaneWizardTitle.setText("<strong>Setup Wizard</strong>");
		panel.add(textPaneWizardTitle);
		textPaneWizardTitle.setEditable(false);

		JSeparator separator = new JSeparator();
		panel.add(separator);

		panel_Content.setBackground(Color.WHITE);
		panel_WizardBar.add(panel_Content);

		JPanel panel_Navigation = new JPanel();
		panel_Navigation.setBackground(Color.WHITE);
		add(panel_Navigation, BorderLayout.SOUTH);
		panel_Navigation.setLayout(new BoxLayout(panel_Navigation, BoxLayout.Y_AXIS));

		JSeparator separator_1 = new JSeparator();
		panel_Navigation.add(separator_1);

		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_1.setBackground(Color.WHITE);
		panel_Navigation.add(panel_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		panel_1.add(panel_2);
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[]{59, 57, 0, 57, 0};
		gbl_panel_2.rowHeights = new int[]{23, 0};
		gbl_panel_2.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel_2.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_2.setLayout(gbl_panel_2);
				GridBagConstraints gbc_btnBack = new GridBagConstraints();
				gbc_btnBack.insets = new Insets(0, 0, 0, 5);
				gbc_btnBack.anchor = GridBagConstraints.NORTHWEST;
				gbc_btnBack.gridx = 0;
				gbc_btnBack.gridy = 0;
				panel_2.add(btnBack, gbc_btnBack);
				
						btnBack.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								back();
							}
						});
				GridBagConstraints gbc_btnNext = new GridBagConstraints();
				gbc_btnNext.anchor = GridBagConstraints.NORTHWEST;
				gbc_btnNext.insets = new Insets(0, 0, 0, 5);
				gbc_btnNext.gridx = 1;
				gbc_btnNext.gridy = 0;
				panel_2.add(btnNext, gbc_btnNext);
				
						btnNext.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								next();
							}
						});
				
				Component horizontalStrut = Box.createHorizontalStrut(20);
				GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
				gbc_horizontalStrut.insets = new Insets(0, 0, 0, 5);
				gbc_horizontalStrut.gridx = 2;
				gbc_horizontalStrut.gridy = 0;
				panel_2.add(horizontalStrut, gbc_horizontalStrut);
				
				
				GridBagConstraints gbc_btnFinish = new GridBagConstraints();
				gbc_btnFinish.anchor = GridBagConstraints.NORTHWEST;
				gbc_btnFinish.gridx = 3;
				gbc_btnFinish.gridy = 0;
				btnFinish.setEnabled(false);
				panel_2.add(btnFinish, gbc_btnFinish);

		if (this.listOfPanels != null && !this.listOfPanels.isEmpty())
			setup();
	}

	private void setup() {
		panel_Content.removeAll();
		panel_Content.add(listOfPanels.get(idx));		
		validateBtnAccess();
		panel_Content.repaint();
		parentFrame.pack();
	}

	private void next() {
		if (idx < listOfPanels.size() - 1) {
			idx++;
			setup();

		}
	}

	private void back() {
		if (idx > 0) {
			idx--;
			setup();
		}
	}

	/**
	 * enable the rigth buttons
	 */
	private void validateBtnAccess() {
		btnBack.setEnabled(idx > 0);
		btnNext.setEnabled(idx < listOfPanels.size()-1);
		btnFinish.setEnabled(idx == listOfPanels.size()-1);
	}

}
